//
//  PageTitleView.swift
//  swiftDemo
//
//  Created by Gerry on 2021/11/8.
//

import UIKit
import RxSwift

private let normalColor : (CGFloat , CGFloat, CGFloat) = (85, 85, 85)
private let selectedColor : (CGFloat, CGFloat, CGFloat) = (255, 128, 0)

protocol PageTitleViewDelegate : AnyObject {
    func pageTitleView(pageTitleView: PageTitleView, selectedIndex index : Int)
}

private let kScrollLineH : CGFloat = 2

class PageTitleView: UIView {

    private var currentIndex : Int = 0
    private var titles : [String]
    weak var delegate : PageTitleViewDelegate?
    
    private lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        
        return scrollView
    }()
    
    private lazy var scrollLine : UIView = {
        let scrollLine = UIView()
        scrollLine.backgroundColor = .orange
        return scrollLine
    }()
    
    private lazy var labels : [UILabel] = [UILabel]()
    
    init(frame: CGRect,  titles:[String]) {
        self.titles = titles
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension PageTitleView {
    
    private func setupUI() {
        addSubview(scrollView)
        scrollView.frame = bounds
        
        setLabels()
     
        setBottomLineAndScrollLine()
    }
    
    private func setLabels() {
        let w = frame.width / CGFloat(titles.count)
        for (index, title) in titles.enumerated() {
            let lab = UILabel()
            lab.text = title;
            lab.textAlignment = .center
            lab.font = UIFont.systemFont(ofSize: 16)
            lab.textColor = UIColor.init(r: normalColor.0, g: normalColor.1, b: normalColor.2)
            lab.tag = index
            lab.frame = CGRect.init(x: CGFloat(index)*w, y: 0, width: w, height: bounds.height-kScrollLineH)
            
            scrollView.addSubview(lab)
            labels.append(lab)
            
            lab.isUserInteractionEnabled = true
            let tapGesture = UITapGestureRecognizer.init(target: self,
                                                         action: #selector(self.tapEvent(tapGesture:)))
            lab.addGestureRecognizer(tapGesture)
        }
    }
    
    private func setBottomLineAndScrollLine() {
        
        addSubview(scrollLine)
        guard let firstLab = labels.first else {
            return
        }
        firstLab.textColor = UIColor.init(r: selectedColor.0, g: selectedColor.1, b: selectedColor.2)
        scrollLine.frame = CGRect(x: firstLab.frame.origin.x, y: bounds.height-kScrollLineH, width: firstLab.frame.width, height: kScrollLineH)
        
        let line = UILabel()
        line.backgroundColor = .lightGray
        line.frame = CGRect(x: 0, y: bounds.height-0.5, width: bounds.width, height: 0.5)
        addSubview(line)
    }
}

extension PageTitleView {
    @objc private func tapEvent(tapGesture : UITapGestureRecognizer) {
        print(tapGesture)
        
        guard let currentLab = tapGesture.view as? UILabel else {
            return
        }
                
        let oldLab = labels[currentIndex]
        oldLab.textColor = UIColor.init(r: normalColor.0, g: normalColor.1, b: normalColor.2)
        currentLab.textColor = UIColor.init(r: selectedColor.0, g: selectedColor.1, b: selectedColor.2)
        currentIndex = currentLab.tag
        
        let x = scrollLine.frame.width * CGFloat(currentIndex)
        UIView.animate(withDuration: 0.2) {            
            self.scrollLine.frame.origin.x = x
        }
        
        delegate?.pageTitleView(pageTitleView: self, selectedIndex: currentIndex)
    }
}

extension PageTitleView {
    func setCurrentTabIndex(progress: CGFloat, scrollDirection : ScrollDirection) {
        var destIndex = 0
        
        switch scrollDirection {
        case .kDirectionToLeft:
            destIndex = currentIndex + 1
            if (destIndex > titles.count - 1) {
                destIndex = titles.count - 1
            }
        case .kDirectionToRight:
            destIndex = currentIndex - 1
            if (destIndex < 0) {
                destIndex = 0
            }
        }
        
        let currLab = labels[currentIndex]
        let destLab = labels[destIndex]
        
        let colorDelta = (selectedColor.0 - normalColor.0, selectedColor.1 - normalColor.1, selectedColor.2 - normalColor.2)
        currLab.textColor = UIColor.init(r: (selectedColor.0 - colorDelta.0 * abs(progress)),
                                         g: (selectedColor.1 - colorDelta.1 * abs(progress)),
                                         b: (selectedColor.2 - colorDelta.2 * abs(progress)))
        
        destLab.textColor = UIColor.init(r: (normalColor.0 + colorDelta.0 * abs(progress)),
                                         g: (normalColor.1 + colorDelta.1 * abs(progress)),
                                         b: (normalColor.2 + colorDelta.2 * abs(progress)))
        
        let off = destLab.frame.origin.x - currLab.frame.origin.x
        self.scrollLine.frame.origin.x = (scrollDirection == .kDirectionToLeft) ? currLab.frame.origin.x + off * progress : currLab.frame.origin.x + progress * abs(off)
        
        if (abs(progress) == 1) {
            currentIndex = destIndex
        }
    }
}
